package gdonly.top.signin.utils;

import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhuzhuxia on 2017/4/27.
 */
public class HttpUtil {
    public static String post(RequestBody body, String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(20, TimeUnit.SECONDS);
        client.setWriteTimeout(20,TimeUnit.SECONDS);
        client.setWriteTimeout(20,TimeUnit.SECONDS);
        Request request=new Request.Builder()
                .url(url)
                .post(body)
                .build();

        Response response = client.newCall(request).execute();

        if (response.isSuccessful()) {
            return response.body().string();
        }
        else {
            Log.e("TAG","code:"+response);
            throw new IOException("Unexpected code " + response);
        }
    }
}
